package com.bestescortbedsystem.controller.userorder;

import com.bestescortbedsystem.service.BedOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: UserOrderController
 * Package: com.bestescortbedsystem.controller.userorder
 *
 * @author: guanzepeng
 * @Date: 2021/3/2 17:14 星期二
 * @Description: 用户订单controller
 */
@RestController
@Slf4j
@Api(tags = "用户订单controller")
@RequestMapping("/userorder")
public class UserOrderController {

	@Resource
	private BedOrderService bedOrderService;

	/**
	 * 根据医院ID和科室ID查询所对应的床位信息
	 * @param hospitalinfoid 传入医院ID
	 * @param departmentid 传入科室ID
	 * @return 返回Map
	 */
	@PostMapping("/findbed")
	@ApiOperation(value = "根据医院ID和科室ID查询所对应的床位信息", response = Map.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "hospitalinfoid", value = "传入医院ID", required = true, dataTypeClass = Long.class),
			@ApiImplicitParam(name = "departmentid", value = "传入科室ID", required = true, dataTypeClass = Long.class)
	})
	public Map<String, Object> findBed(Long hospitalinfoid, Long departmentid) {
		Map<String, Object> bed = bedOrderService.findBed(hospitalinfoid, departmentid);
		return bed;
	}

	/**
	 * 生成订单
	 * @param bedtypePrice 传入床位单价
	 * @param bedmessageId 传入床位租借信息表ID
	 * @param bedtypeInventory 传入现有库存
	 * @return 返回Map
	 */
	@PostMapping("/byorder")
	@ApiOperation(value = "生成订单", response = Map.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "bedtypePrice", value = "传入床位单价", required = true, dataTypeClass = String.class),
			@ApiImplicitParam(name = "bedmessageId", value = "传入床位租借信息表ID", required = true, dataTypeClass = String.class),
			@ApiImplicitParam(name = "bedtypeInventory", value = "传入现有库存", required = true, dataTypeClass = String.class),
			@ApiImplicitParam(name = "money", value = "传入总计金额", required = true, dataTypeClass = Float.class),
			@ApiImplicitParam(name = "tian", value = "传入天数", required = true, dataTypeClass = String.class)
	})
	public Map<String, String> byOrder(HttpSession httpSession, String bedtypePrice, String bedmessageId, String bedtypeInventory, Float money, String tian) {
		String userId = (String) httpSession.getAttribute("userId2");
		if(userId == null) {
			Map<String, String> map = new HashMap<>();
			map.put("s", "请登录!");
			return map;
		}
		Map<String, String> map = bedOrderService.byOrder(Long.valueOf(bedtypePrice), Long.valueOf (bedmessageId), Long.valueOf (bedtypeInventory), money, tian, Long.valueOf(userId));
		return map;
	}

	/**
	 * 修改订单付款状态
	 * @param userorderId 传入订单ID
	 * @param userorderStatus 传入修改状态
	 * @return 返回Boolean
	 */
	@PostMapping("/updatestatus")
	@ApiOperation(value = "修改订单付款状态", response = Boolean.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userorderId", value = "传入订单ID", required = true, dataTypeClass = Long.class),
			@ApiImplicitParam(name = "userorderStatus", value = "传入修改状态", required = true, dataTypeClass = Integer.class)
	})
	public Boolean updateStatus(Long userorderId, Integer userorderStatus) {
		Boolean b = bedOrderService.updateStatus(userorderId, userorderStatus);
		return b;
	}

	/**
	 * 根据下单天数扣除账户金额
	 * @param money 传入总价
	 * @param tian 下单天数
	 * @param userorderId 传入订单表ID
	 * @return 返回Boolean
	 */
	@PostMapping("/updateYuE")
	@ApiOperation(value = "根据下单天数扣除账户金额", response = Boolean.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "money", value = "传入总价", required = true, dataTypeClass = Float.class),
			@ApiImplicitParam(name = "tian", value = "下单天数", required = true, dataTypeClass = String.class),
			@ApiImplicitParam(name = "userorderId", value = "传入订单表ID", required = true, dataTypeClass = Long.class)
	})
	public Boolean updateYuE(HttpSession httpSession, Float money, String tian, Long userorderId) {
		String userId = (String) httpSession.getAttribute("userId2");
		if (userId == null) {
			return false;
		}
		Boolean b = bedOrderService.updateYuE(money, tian, Long.valueOf(userId), userorderId);
		return b;
	}
}
